const http = require('../../../utils/util')
const Base64 = require('../../../utils/base')
const {baseUrl}= require('../../../utils/util')
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		desc: '',
		imagesList: [],
		imagesListParams:[]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},
	//选择本地图片
	uploader: function() {
		let _this = this
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function(res) {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths[0]
				console.log(res);
				wx.uploadFile({
					url: baseUrl + '/common/imageUpload', //仅为示例，非真实的接口地址
					filePath: tempFilePaths,
					name: 'file',
					success(res) {
						let data = JSON.parse(res.data)
						if (data.status == 1) {
							_this.setData({
								imagesList: [..._this.data.imagesList, data.result.all_file_path],
								imagesListParams: [..._this.data.imagesListParams, data.result.file_path]
							})
						} else {
							wx.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					}
				})
			}
		})
	},
	//删除选中图片
	deleteImg(e) {
		let index = e.currentTarget.dataset.index
		let imagesList = this.data.imagesList
		let imagesListParams=this.data.imagesListParams
		imagesList.splice(index, 1)
		imagesListParams.splice(index,1)
		this.setData({
			imagesList: imagesList,
			imagesListParams:imagesListParams
		})
	},
	//输入框输入事件
	bindContent(e) {
		this.setData({
			desc: e.detail.value
		})
	},
	//提交
	feedbackUpload() {
		let data = {
			user_id: wx.getStorageSync('user_id'),
			user_token: wx.getStorageSync('user_token'),
			desc: this.data.desc,
			img: this.data.imagesListParams
		}
		http.post('user/opinion', Base64.encode(JSON.stringify(data))).then((res) => {
			if (res.status == 1) {
				wx.showToast({
					title: '提交成功',
					icon: 'none'
				})
				setTimeout(() => {
					wx.navigateBack({
						delta: 1
					})
				}, 1500)
			} else {
				wx.showToast({
					title: res.message,
					icon: 'none'
				})
			}
		})
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
